// miniprogram/pages/index/company/managed_job/show_resume/show_resume.js
const db = wx.cloud.database()
const _=db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _id: '',
    students:[],
    btn: true,
    job: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const _id=options._id;
    const job=JSON.parse(decodeURIComponent(options.job));
    
    this.setData({
      _id: _id,
      job: job
    })
    // db.collection('student').where({
    //   'student_obj.jobs_id': _id
    // }).get().then(res=>{
    //   this.setData({students: res.data})
    // })
    this.loadData()

  },
  loadData: function(){
    const that=this;
    const _id=this.data._id;
    db.collection('student')
   .aggregate()
   .match({'student_obj.jobs_id':  _id})
   .sample({size: 20})
   .end().then(res=>{
     
     that.setData({students: res.list})
     
   })
 },
 check: function(event){
  const that = this;
  let tem="";
  wx.requestSubscribeMessage({
    tmplIds: ['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'], 
    success: res=>{
      tem = res['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'];
      if(tem=="accept"){
          wx.showToast({
          title: '已开启',
        })
        that.pass(event);
      }else{
        wx.showToast({
          title: '已拒绝',
        })
      }
    },
    fail: res=>{
        wx.showToast({
          title: '已拒绝',
        })
    }
  })
},
  pass: function(event){
    const that=this;
    this.setData({btn: true})
    const students=this.data.students;
    const sindex=event.target.dataset.sindex;
    const student=students[sindex]
    const openid=student.student_obj.openid;
    const jobs_id=student.student_obj.jobs_id;
    jobs_id.splice(sindex, 1);
    students.splice(sindex,1);
    this.setData({
      students: students
    })
    this.setData({
      jobs_id: jobs_id
    })
    db.collection('student').doc(student._id).update({
      data: {
        'student_obj.jobs_id': jobs_id,
        
      }
    }).then(res=>{
      // console.log(openid);
      
      // console.log(that.data.job.job_obj.cpny_name);
      // console.log(that.data.job.job_obj.job_name);
      
      wx.cloud.callFunction({
        name: 'notice',
        data: {
          target: openid,
          title: "简历被收取", 
          content: that.data.job.job_obj.cpny_name,
          note:    that.data.job.job_obj.job_name
        }
      })

      getApp().tosSuccess("收取成功")
    })
    this.setData({btn: false})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({btn: false})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
    this.loadData()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})